// by Xeno
#define THIS_FILE "fn_x_teleport.sqf"
#include "x_setup.sqf"
private ["_ok","_vehicle"];

if (vehicle player != player) exitWith {systemChat (localize "STR_DOM_MISSIONSTRING_583")};

if (!isNull (flag player)) exitWith {systemChat (localize "STR_DOM_MISSIONSTRING_584")};

_is_swimmer = ((animationState player) in ["aswmpercmstpsnonwnondnon","aswmpercmstpsnonwnondnon_aswmpercmrunsnonwnondf","aswmpercmrunsnonwnondf_aswmpercmstpsnonwnondnon","aswmpercmrunsnonwnondf","aswmpercmsprsnonwnondf","aswmpercmwlksnonwnondf"]);
if (_is_swimmer) exitWith {systemChat (localize "STR_DOM_MISSIONSTRING_585")};

if (dialog) then {closeDialog 0};

GVAR(beam_target) = -1;
if (GVAR(WithTeleToBase) == 0) then {
	GVAR(tele_dialog) = 2; // 0 = respawn, 1 = teleport
} else {
	GVAR(tele_dialog) = 1;
};

disableSerialization;

createDialog QGVAR(TeleportModule);

#define __CTRL(A) (_display displayCtrl A)

_display = __uiGetVar(X_TELE_DIALOG);
__CTRL(100102) ctrlSetText (localize "STR_DOM_MISSIONSTRING_533");
__CTRL(100111) ctrlSetText (localize "STR_DOM_MISSIONSTRING_586");
if (GVAR(WithTeleToBase) == 1) then {
	__CTRL(100107) ctrlShow false;
};
__CTRL(100112) ctrlShow false;

GVAR(x_loop_end) = false;

[GVAR(last_telepoint)] call FUNC(x_update_target);

0 spawn {
	scriptName "spawn_x_teleport_teleupdate";
	while {!GVAR(x_loop_end) && {alive player} && {dialog}} do {
		if (!GVAR(x_loop_end) && {alive player}) then {call FUNC(teleupdate_dlg)};
		sleep 1.012;
	};
	if (!alive player) then {closeDialog 0};
};